package com.roy.udp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * @author ：楼兰
 * @date ：Created in 2021/8/6
 * @description:
 **/

public class UDPClient {
    public static void main(String[] args) throws IOException {
        final DatagramChannel channel = DatagramChannel.open();
        channel.connect(new InetSocketAddress("localhost",9999));

        String message = "Hello Server. sended from Client ,time ="+System.currentTimeMillis();

        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(message.getBytes());
        buffer.flip();

        final int sended = channel.send(buffer, new InetSocketAddress("localhost", 9999));
        System.out.println("已发送数据 大小 "+sended);
    }
}
